package com.gooddare.view 
{
	import com.gooddare.controller.ApplicationFacade;
	import com.gooddare.locator.ApiLocator;
	import com.gooddare.model.ModelLocator;
	import com.gooddare.model.vo.RuleVO;
	import com.gooddare.view.component.DispicItemComponent;
	import com.greensock.easing.Quad;
	import com.greensock.easing.Sine;
	import com.greensock.TweenMax;
	import com.riatimes.mvc.model.vo.DataParamVO;
	import com.riatimes.net.http.HttpService;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import org.puremvc.as3.interfaces.INotification;
	/**
	 * ...
	 * @author Eric
	 */
	public class DispicFrameMediator extends BaseView
	{
		public static var NAME:String = "DispicFrameMediator";
		private var dispicFrame:Sprite;
		private static const displayListNum:int = 5;//显示出来的个数
		private var ln:int;//左右的个数  显示出来的个数/2；
		
		private var vo:RuleVO;
		private var voLength:int;
		private var nArr:Array;
		private var mArr:Array;
		private var proArr:Array;
		private var displayArr:Array;
		private var at:int=0;
		private var lt:int=0;
		
		private var xArr:Array = [-200, -130, 0, 130, 200]
		private var ryArr:Array = [ -20, -27, 0, 27, 20];
		private var scaleArr:Array = [0.75, 0.8, 1, 0.8, 0.75];
		public function DispicFrameMediator(name:String=null,viewComponent:Object=null) 
		{
			if (name != null) NAME = name;
			if (viewComponent == null) viewComponent = new Sprite();
			super(NAME, viewComponent);
			dispicFrame = getView();
			dispicFrame.x = 480;
			dispicFrame.y = 45;
			ModelLocator.ROOT.addChildAt(dispicFrame, 0);

		}
		override public function listNotificationInterests():Array {
			return[
				ApplicationFacade.EVENT_APP_PHOTOPAGE_GET_INFO,
				ApplicationFacade.EVENT_APP_PHOTO_DISPLAY_GET_INDEX,
				ApplicationFacade.EVENT_APP_QUITSTAGE_CLICK,
			]
		}
		override public function handleNotification(note:INotification):void {
			var data:Object = note.getBody();
			switch(note.getName()) {
				case ApplicationFacade.EVENT_APP_PHOTOPAGE_GET_INFO:
					var vo:RuleVO = new RuleVO(data);
					this.vo = vo;
					voLength = vo.photolist.length;
					initUI();
					break;
				case ApplicationFacade.EVENT_APP_PHOTO_DISPLAY_GET_INDEX:
					TweenMax.killChildTweensOf(dispicFrame, true);
					var index:int = int(data);
					createDataArrayByIndex(index);
					break;
				case ApplicationFacade.EVENT_APP_QUITSTAGE_CLICK:
					clearFrame();
					break;
			}
		}
		
		private function initUI():void 
		{
			ln = int(displayListNum / 2);
			proArr = new Array();
			nArr = new Array();
			mArr = new Array();
			displayArr = new Array();
			createDataArrayByIndex(0);
		}
		
		private function createDataArrayByIndex(index:int):void 
		{
			mArr = [];
			var i:int;
			lt = at;
			at = index;
			for (i = at - ln; i <=at+ln; i++ ) {
				var ix:int;
				if (i >= 0&&i<voLength) ix = i;
				else ix = Math.abs((voLength - Math.abs(i))) % voLength;
				mArr.push(ix);
				if (nArr.indexOf(ix) == -1) {//如果该数字不存在于nArr数组中，将它添加到临时数组
					proArr.push(ix);
				}
			}
			wathWay();//插入位置
		}
		
		/**
		 * 插入位置，左边和右边
		 */
		private function wathWay():void 
		{
			var pdArr:Array = new Array();
			for (var i:int = 0, j:int = lt; i < voLength / 2; i++,j++ ) {
				pdArr.push(j % voLength);
			}
			if (pdArr.indexOf(at) == -1) {//trace("左边插入")
				instalAtLeft();
			}else {//trace("右边插入")
				instalAtRight();
			}
			pdArr = [];
		}
		
		private function instalAtLeft():void 
		{
			for (var i:int = proArr.length - 1; proArr.length > 0; i = proArr.length - 1 ) {
				var mc:MovieClip = createMc(proArr[i],true);
				//mc.x = xArr[voLength-1];
				nArr.unshift(proArr[i]);
				displayArr.unshift(mc);
				proArr.pop();
			}
			removeOtherArr(false);//左插，说明无用元素向右移动
		}
		
		private function instalAtRight():void 
		{
			for (var i:int = 0; proArr.length > 0; ) {
				var mc:MovieClip = createMc(proArr[0],false);
				//mc.x = xArr[0];
				nArr.push(proArr[0]);
				displayArr.push(mc);
				proArr.shift();
			}
			removeOtherArr(true);//右插，说明无用元素向左移动
		}
		
		/**
		 * 取出display中多余的元素
		 */
		private function removeOtherArr(r:Boolean=false):void 
		{
			var otherArr:Array = new Array();//显示之外的无用元素
			var usefulArr:Array = new Array();//显示之内的有用元素
			for (var i:int = 0; i < displayArr.length; i++ ) {
				if (mArr.indexOf(displayArr[i].id) == -1) {//不存在于mArr，则为无用元素
					otherArr.push(displayArr[i]);
				}else {
					usefulArr.push(displayArr[i]);
				}
			}
			
			nArr = mArr;
			displayArr = usefulArr;
			otherTween(otherArr, r);
			usefullTween(usefulArr);
		}
		
		private function usefullTween(usefulArr:Array):void 
		{
			dispicFrame.setChildIndex(usefulArr[2], dispicFrame.numChildren - 1);
			dispicFrame.setChildIndex(usefulArr[1], dispicFrame.numChildren - 2);
			dispicFrame.setChildIndex(usefulArr[3], dispicFrame.numChildren - 3);
			for (var i:int = 0; i < usefulArr.length; i++ ) {
				//(usefulArr[i] as Sprite).rotationY
				TweenMax.to(usefulArr[i], 0.5, { alpha:1, x:xArr[i], shortRotation:{rotationY:ryArr[i]}, scaleX:scaleArr[i], scaleY:scaleArr[i] } );
			}
			
		}
		
		public function removeOtherEle(id:int,otherArr:Array):void 
		{
			if (id == otherArr.length-1) {
				for (var i:int = 0; i < otherArr.length; i++ ) {
					dispicFrame.removeChild(otherArr[i]);
				}
			}
		}
		
		/**
		 * 无用元素移动
		 * @param	r 如果为false,向右移动, 如果为true，向左移动
		 */
		private function otherTween(otherArr:Array,r:Boolean=false):void 
		{
			var i:int;
			var len:int = otherArr.length;
			if (r) {//向左
				for (i= 0; i < len; i++ ) {
					TweenMax.to(otherArr[i], 0.5, { alpha:0,x:0,onComplete:removeOtherEle,onCompleteParams:[i,otherArr] } );
				}
			}else {//向右
				for (i = 0; i < len; i++ ) {
					TweenMax.to(otherArr[len - 1 - i], 0.5, {alpha:0, x:xArr[voLength-1],onComplete:removeOtherEle,onCompleteParams:[i,otherArr]} );
				}
			}
		}
		
		private function createMc(id:int,r:Boolean=true):MovieClip {
			var mc:MovieClip = new MovieClip();
			var dic:DispicItemComponent = new DispicItemComponent(id, vo.photolist[id].linkimg);
			mc.addChild(dic);
			mc.id = id;
			mc.y = 250;
			mc.scaleX = mc.scaleY = scaleArr[0];
			mc.alpha = 0;
			if (r) {
				mc.rotationY = -90;
			}else {
				mc.rotationY = 90;
			}
			
			dispicFrame.addChild(mc);
			return mc as MovieClip;
		}
		
				
		private function clearFrame():void 
		{
			ModelLocator.ROOT.removeChild(dispicFrame);
			facade.removeMediator(NAME);
		}
	}

}